import {ref} from "vue";
import {defineStore} from "pinia";
import {
    delShoppingCart,
    getShoppingCartList,
    newAddShoppingCartAdd,
    newShoppingCartSub
} from "@/apis/shoppingCartApi.js";
export const useCartStore = defineStore("cartStore", ()=>{
    const cartList=ref([]);
    const getCartList=async ()=>{
        const res=await getShoppingCartList();
        console.log(res.data);
        cartList.value = res.data;
    }
    const addCart =async (dishid)=>{
        const params=ref({
            "dishId": 0,
            "setmealId": 0,
            "dishFlavor": ""
        })
        params.value.dishId=dishid
        await newAddShoppingCartAdd(params.value)
        await getCartList()
    }
    const subCart =async (dishid)=>{
        const params=ref({
            "dishId": 0,
            "setmealId": 0,
            "dishFlavor": ""
        })
        params.value.dishId=dishid
        await newShoppingCartSub(params.value)
        await getCartList()
    }
    const delCart =async ()=>{

        await delShoppingCart()
        await getCartList()
    }

    return {
        cartList,
        addCart,
        subCart,
        delCart,
        getCartList
    }
},{
    //开启刷新防丢失
    persist: true,
})